{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"一键开门\", \"一键上锁\", \"蓝牙钥匙\", \"位置\", \"理赔\", \"维修\", \"车辆信息\", \"车辆安全\"]\n",
    "actvt = [\"OneKeyLock\", \"OneKeyUnlock\", \"BluetoothKey\", \"CarPosition\", \"ClaimPay\", \"Repair\", \"CarInfo\", \"CarSecurity\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "template = \"\"\"package com.xfcar.driver.view;\n",
    "\n",
    "import android.os.Bundle;\n",
    "import android.view.View;\n",
    "\n",
    "import com.xfcar.driver.R;\n",
    "import com.xfcar.driver.mvp.BaseActivity;\n",
    "\n",
    "public class {}Activity extends BaseActivity implements View.OnClickListener {{\n",
    "\n",
    "    @Override\n",
    "    protected void onCreate(Bundle savedInstanceState) {{\n",
    "        super.onCreate(savedInstanceState);\n",
    "        setContentView(R.layout.activity{});\n",
    "        initView();\n",
    "    }}\n",
    "\n",
    "    private void initView() {{\n",
    "        findViewById(R.id.iv_return_back).setOnClickListener(this);\n",
    "    }}\n",
    "\n",
    "    @Override\n",
    "    public void onClick(View v) {{\n",
    "        int id = v.getId();\n",
    "        if (id == R.id.iv_return_back) {{\n",
    "            finish();\n",
    "        }}\n",
    "    }}\n",
    "}}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_xml_name(actvt):\n",
    "    return \"\".join([\"_\" + i.lower() if i.isupper() else i for i in actvt])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(names)):\n",
    "    file_name = \"./app/src/main/java/com/xfcar/driver/view/\" + actvt[i] + \"Activity.java\"\n",
    "    with open(file_name, \"w\") as f:\n",
    "        f.write(template.format(actvt[i], get_xml_name(actvt[i])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "} else if (appBean.name.equals(\"一键开门\")) {\n",
      "startActivity(OneKeyLockActivity.class);\n",
      "} else if (appBean.name.equals(\"一键上锁\")) {\n",
      "startActivity(OneKeyUnlockActivity.class);\n",
      "} else if (appBean.name.equals(\"蓝牙钥匙\")) {\n",
      "startActivity(BluetoothKeyActivity.class);\n",
      "} else if (appBean.name.equals(\"位置\")) {\n",
      "startActivity(CarPositionActivity.class);\n",
      "} else if (appBean.name.equals(\"理赔\")) {\n",
      "startActivity(ClaimPayActivity.class);\n",
      "} else if (appBean.name.equals(\"维修\")) {\n",
      "startActivity(RepairActivity.class);\n",
      "} else if (appBean.name.equals(\"车辆信息\")) {\n",
      "startActivity(CarInfoActivity.class);\n",
      "} else if (appBean.name.equals(\"车辆安全\")) {\n",
      "startActivity(CarSecurityActivity.class);\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(names)):\n",
    "    print('}} else if (appBean.name.equals(\"{}\")) {{'.format(names[i]))\n",
    "    print('startActivity({}Activity.class);'.format(actvt[i]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "xml_template = \"\"\"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
    "<LinearLayout\n",
    "    xmlns:android=\"http://schemas.android.com/apk/res/android\"\n",
    "    android:layout_width=\"match_parent\"\n",
    "    android:layout_height=\"match_parent\"\n",
    "    android:orientation=\"vertical\"\n",
    ">\n",
    "\n",
    "    <RelativeLayout\n",
    "        android:layout_width=\"match_parent\"\n",
    "        android:layout_height=\"@dimen/space60\"\n",
    "    >\n",
    "\n",
    "        <ImageView\n",
    "            android:id=\"@+id/iv_return_back\"\n",
    "            android:layout_width=\"wrap_content\"\n",
    "            android:layout_height=\"wrap_content\"\n",
    "            android:src=\"@mipmap/ic_return_back\"\n",
    "            android:layout_centerVertical=\"true\"\n",
    "            android:layout_marginLeft=\"@dimen/space20\"\n",
    "        />\n",
    "\n",
    "        <TextView\n",
    "            android:id=\"@+id/tv_title\"\n",
    "            android:layout_width=\"wrap_content\"\n",
    "            android:layout_height=\"wrap_content\"\n",
    "            android:layout_centerInParent=\"true\"\n",
    "            android:text=\"{}\"\n",
    "            android:textColor=\"@color/black\"\n",
    "            android:textSize=\"@dimen/font16\"\n",
    "        />\n",
    "\n",
    "    </RelativeLayout>\n",
    "    \n",
    "    <View\n",
    "        android:layout_width=\"match_parent\"\n",
    "        android:layout_height=\"@dimen/space1\"\n",
    "        android:background=\"@color/gray\"\n",
    "    />\n",
    "\n",
    "    <android.support.v7.widget.RecyclerView\n",
    "        android:id=\"@+id/rv_car_function\"\n",
    "        android:layout_width=\"match_parent\"\n",
    "        android:layout_height=\"0dp\"\n",
    "        android:layout_weight=\"1.0\"\n",
    "        android:layout_marginLeft=\"@dimen/space15\"\n",
    "        android:layout_marginRight=\"@dimen/space15\"\n",
    "    />\n",
    "\n",
    "</LinearLayout>\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_xml_name(actvt):\n",
    "    return \"\".join([\"_\" + i.lower() if i.isupper() else i for i in actvt])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(names)):\n",
    "    xml_file_name = \"activity\" + get_xml_name(actvt[i]) + \".xml\"\n",
    "    with open(\"./app/src/main/res/layout/{}\".format(xml_file_name), \"w\") as f:\n",
    "        f.write(xml_template.format(names[i]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "manifest_template = \"\"\"\n",
    "<activity\n",
    "            android:name=\".view.{}Activity\"\n",
    "            android:screenOrientation=\"portrait\"\n",
    "        />\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "<activity\n",
      "            android:name=\".view.OneKeyLockActivity\"\n",
      "            android:screenOrientation=\"portrait\"\n",
      "        />\n",
      "\n",
      "\n",
      "<activity\n",
      "            android:name=\".view.OneKeyUnlockActivity\"\n",
      "            android:screenOrientation=\"portrait\"\n",
      "        />\n",
      "\n",
      "\n",
      "<activity\n",
      "            android:name=\".view.BluetoothKeyActivity\"\n",
      "            android:screenOrientation=\"portrait\"\n",
      "        />\n",
      "\n",
      "\n",
      "<activity\n",
      "            android:name=\".view.CarPositionActivity\"\n",
      "            android:screenOrientation=\"portrait\"\n",
      "        />\n",
      "\n",
      "\n",
      "<activity\n",
      "            android:name=\".view.ClaimPayActivity\"\n",
      "            android:screenOrientation=\"portrait\"\n",
      "        />\n",
      "\n",
      "\n",
      "<activity\n",
      "            android:name=\".view.RepairActivity\"\n",
      "            android:screenOrientation=\"portrait\"\n",
      "        />\n",
      "\n",
      "\n",
      "<activity\n",
      "            android:name=\".view.CarInfoActivity\"\n",
      "            android:screenOrientation=\"portrait\"\n",
      "        />\n",
      "\n",
      "\n",
      "<activity\n",
      "            android:name=\".view.CarSecurityActivity\"\n",
      "            android:screenOrientation=\"portrait\"\n",
      "        />\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for act in actvt:\n",
    "    print(manifest_template.format(act))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
